package com.yanq.liancar.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;

import com.yanq.liancar.R;
import com.yanq.liancar.bean.NoScrollViewPager;
import com.yanq.liancar.fragment.SelectAddressFragment;
import com.yanq.liancar.view.verticaltablayout.QTabView;
import com.yanq.liancar.view.verticaltablayout.TabAdapter;
import com.yanq.liancar.view.verticaltablayout.TabView;
import com.yanq.liancar.view.verticaltablayout.VerticalTabLayout;

import java.util.ArrayList;
import java.util.List;


public class SelectAddressActivity extends FragmentActivity {
    private String[] sheng;
    private VerticalTabLayout mVerticalTabLayout;
    private NoScrollViewPager mViewPager;
    private List<String> menus;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private int mPos = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_address);
        sheng = new String[]{"北京（京）", "安徽（皖）", "山东（鲁）", "天津（津）", "河北（冀）", "山西（晋）", "内蒙古（蒙）", "辽宁（辽）", "吉林（吉）", "黑龙江（黑）", "上海（沪）", "江苏（苏）", "浙江（浙）", "福建（闽）", "江西（赣）", "河南（豫）"
               , "湖北省（鄂）", "湖南省（湘）", "广东省（粤）", "广西壮族自治区（桂）", "海南省（琼）",  "四川省（川）", "贵州省（贵）", "云南省（云）", "西藏自治区（藏）", "陕西省（陕）", "甘肃省（甘）", "青海省（青）", "宁夏回族自治区（宁）", "新疆维吾尔自治区（新）","重庆市（渝）"};
        mVerticalTabLayout = (VerticalTabLayout) findViewById(R.id.vertical_tab);
        mViewPager = (NoScrollViewPager) findViewById(R.id.vp_address);


        menus = new ArrayList<>();
        for (int i = 0; i < sheng.length; i++) {
            menus.add(sheng[i]);
            mFragments.add(SelectAddressFragment.getInstance(i));

        }
        mVerticalTabLayout.setTabAdapter(new MyTabAdapter(menus));
        mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        mVerticalTabLayout.setupWithViewPager(mViewPager);
        mViewPager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mVerticalTabLayout.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
//                SelectAddressFragment.getInstance(position);
            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return sheng[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

    private class MyTabAdapter implements TabAdapter {

        List<String> menus;

        public MyTabAdapter(List<String> menus) {
            this.menus = menus;
        }

        @Override
        public int getCount() {
            return menus.size();
        }

        @Override
        public int getBadge(int position) {
            if (position == menus.size()) return position;

            return 0;
        }


        @Override
        public QTabView.TabIcon getIcon(int position) {
            return null;
        }


        @Override
        public QTabView.TabTitle getTitle(int position) {
            String menu = menus.get(position);
            return new QTabView.TabTitle.Builder(SelectAddressActivity.this)
                    .setContent(menu)
                    //.setTextColor(Color.BLUE, Color.BLACK)
                    .build();
        }

        @Override
        public int getBackground(int position) {
            return 0;
        }

    }

}
